//
//  WorkflowApplyViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/30.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "WorkflowApplyViewController.h"
#import "WorkflowSelectUserViewController.h"

NSString *const WorkflowApplyViewControllerWorkflowInfoParametersParamKey = @"WorkflowApplyViewControllerWorkflowInfoParametersParamKey";

@interface WorkflowApplyViewController ()
{
    WorkflowInfo *_workflowInfo;
    WorkflowApplyInfo *_workApplyInfo;
    
    RSWDataOperation *_manager;
}
@end

@implementation WorkflowApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    [self checkSession];
    
    _workflowInfo = self.param[WorkflowApplyViewControllerWorkflowInfoParametersParamKey];
    
    [self initVariable];
    [self initUI];
    [_manager getWorkflowApply:_workflowInfo.processCode customform:_workflowInfo.customFormID version:_workflowInfo.version completion:^(NSInteger nResult, id output, NSError *error) {
        [self requestComplete:nil result:nResult output:output error:error];
    }];
}

-(void)initVariable
{
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
    
    _vclType = DynamicColumnFieldViewTypeAdd;
    _transformMap = nil;
}

-(void)initUI
{
    [super initViewDidLoad];
    self.navTitle = _workflowInfo.processName;
}

-(void)requestComplete:(XMSDataOperationManager *)dom result:(NSInteger)nResult output:(id)output error:(NSError *)error
{
    [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
     {
         switch(requestResult)
         {
             case YES:
             {
                 _workApplyInfo = output;
                 if(_workApplyInfo.fields.count > 0)
                 {
                     _constraint = [WorkAppInfo constraint:_workApplyInfo];
                     
                     if(_workApplyInfo.nextActionNames.count > 0)
                     {
                         self.rightBarButtonItem.title = _workApplyInfo.nextActionNames[0];
                     }
                     [super reloadDyanmicField];
                 }
                 break;
             }
             default:
             {
                 break;
             }
         }
     }];
}

- (CGFloat)dynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore heightForHeaderInGroup:(NSInteger)group
{
    return 0;
}

-(void)clickedRightBarButton:(id)sender
{
    [UIViewController closeKeyBoard];
    
    if([super verifyDynamicField:_constraint] == YES)
    {
        CommonObjInfo *modelInfo = [CommonObjInfo new];
        
        [modelInfo.expandfields setValue:_workApplyInfo.customForm.customerFormId forKey:@"Slickflow_CustomForm2ID"];
        [modelInfo.expandfields setValue:_workApplyInfo.version forKey:@"Slickflow_Version"];
        [modelInfo.expandfields setValue:_workApplyInfo.processGUID forKey:@"Slickflow_ProcessGUID"];
        [modelInfo.expandfields setValue:_workflowInfo.processCode forKey:@"Slickflow_ProcessCode"];
        
        //下一步提交按钮
        [modelInfo.expandfields setValue:_workApplyInfo.nextActionNames[0] forKey:@"Button"];
        
        XMSQueueDictionary *qdSet = _transformMap;
        
        [super extractDynamicFieldValue:_constraint transformMap:qdSet packObj:modelInfo specialExtract:^BOOL(FieldDescript *fixField)
         {
             return NO;
         }];
        
        XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
        
        NSMutableDictionary *dic = [NSMutableDictionary new];
        NSString *appInstanceId = modelInfo.expandfields[@"ID"];
        [dic setValue:appInstanceId forKey:@"AppInstanceID"];
        [dic setValue:_workflowInfo.processName forKey:@"AppName"];
        [dic setValue:modelInfo.expandfields forKey:@"Conditions"];
        [dic setValue:[[MyInfo myInfo].userNumber toNSString] forKey:@"CreateUserID"];
        [dic setValue:[MyInfo myInfo].userName forKey:@"CreateUserName"];
        [dic setValue:@"#NEXT#" forKey:@"NextActivityPerformers"];
        [dic setValue:_workflowInfo.processGUID forKey:@"ProcessGUID"];
        [dic setValue:[[MyInfo myInfo].userNumber toNSString] forKey:@"UserID"];
        [dic setValue:[MyInfo myInfo].userName forKey:@"UserName"];
        [dic setValue:_workflowInfo.version forKey:@"Version"];
        [dic setValue:@(0) forKey:@"themeColor"];
        [dic setValue:_workApplyInfo.attachments forKey:@"attachment"];
        
        p.param[WorkflowSelectUserViewControllerFormDicParametersParamKey] = dic;
        p.param[WorkflowSelectUserViewControllerOptTypeParametersParamKey] = @(WorkflowOptTypeApply);
        [XUINavigationController pushViewControllerClass:[WorkflowSelectUserViewController class] withSwitchParam:p];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
